using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RelatedPostPlugin
{
    public partial class InsertForm : Form
    {
        public InsertForm()
        {
            InitializeComponent();
        }

        public List<string> Params
        {
            get
            {
                List<string> tmp = new List<string>(3);
                tmp.Add(url_.Text);
                tmp.Add(text_.Text);
                tmp.Add(title_.Text);
                return tmp;
            }
        }

        public void SetItems(string url, string text, string title)
        {
            url_.Text = url;
            text_.Text = text;
            title_.Text = title;
        }

        private void url__KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckForExit(e.KeyChar);
            if (e.KeyChar == (char)Keys.Return)
            {
                if (!CheckForEmpty() && CheckUrl(url_.Text))
                {
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
                else SetErrorProviders();
            }
        }

        private void text__KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckForExit(e.KeyChar);
            if (e.KeyChar == (char)Keys.Return)
            {
                if (!CheckForEmpty() && CheckUrl(url_.Text))
                {
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
                else SetErrorProviders();
            }
        }

        private void title__KeyPress(object sender, KeyPressEventArgs e)
        {
            CheckForExit(e.KeyChar);
            if (e.KeyChar == (char)Keys.Return)
            {
                if (!CheckForEmpty() && CheckUrl(url_.Text))
                {
                    this.DialogResult = DialogResult.OK;
                    Close();
                }
                else SetErrorProviders();
            }
        }

        private void CheckForExit(char pressed)
        {
            if (pressed == (char)Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Alt | Keys.U:
                    url_.Focus();
                    url_.SelectAll();
                    break;
                case Keys.Alt | Keys.T:
                    text_.Focus();
                    text_.SelectAll();
                    break;
                case Keys.Alt | Keys.I:
                    title_.Focus();
                    title_.SelectAll();
                    break;
                default:
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void feedback__LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                feedback_.LinkVisited = true;
                System.Diagnostics.Process.Start("mailto:dmtmax@gmail.com");
            }
            catch (Exception)
            {
                MessageBox.Show("Some errors was occured");
            }
        }

        private bool CheckForEmpty()
        {
            bool res = (url_.Text == string.Empty) || (text_.Text == string.Empty);
            return res;
        }

        private bool CheckUrl(string url)
        {
            if (url == string.Empty) return false;
            return IsEmail(url) || IsUrl(url);
        }

        public static bool IsEmail(string Email)
        {
            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);
            if (re.IsMatch(Email))
                return (true);
            else
                return (false);
        }

        public static bool IsUrl(string Url)
        {
            string strRegex = "(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))";
            Regex re = new Regex(strRegex);

            if (re.IsMatch(Url))
                return (true);
            else
                return (false);
        }

        private void btnOk__Click(object sender, EventArgs e)
        {
            if (!CheckForEmpty() && CheckUrl(url_.Text))
            {
                this.DialogResult = DialogResult.OK;
                Close();
            }
            else SetErrorProviders();
        }

        private void SetErrorProviders()
        {
            if (url_.Text == string.Empty)
                urlError_.SetError(url_, "The Url field cannot be empty");
            else if (!CheckUrl(url_.Text))
                urlError_.SetError(url_, "The entered Url is not valid");
            else urlError_.SetError(url_, "");
            if (text_.Text == string.Empty)
                textError_.SetError(text_, "The link text cannot be empty");
            else textError_.SetError(text_, "");
        }

        private void InsertForm_Shown(object sender, EventArgs e)
        {
            if (url_.Text != string.Empty)
                text_.Focus();
        }

        private void InsertForm_Load(object sender, EventArgs e)
        {
            tt_.SetToolTip(url_, "Enter a valid Url (this field cannot be empty");
            tt_.SetToolTip(text_, "Enter a link text (this field cannot be empty)");
            tt_.SetToolTip(title_, "Enter a link title (this field can be empty)");
        }
    }
}